package team.seekers.hula.course.resource.sectionContentComment.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import team.seekers.hula.common.entity.PageResult;
import team.seekers.hula.course.resource.sectionContent.dao.SectionContentDao;
import team.seekers.hula.course.resource.sectionContent.entity.dao.SectionContentDO;
import team.seekers.hula.course.resource.sectionContent.entity.dto.SectionContentDTO;
import team.seekers.hula.course.resource.sectionContent.mapper.SectionContentMapper;
import team.seekers.hula.course.resource.sectionContent.service.SectionContentService;
import team.seekers.hula.course.resource.sectionContentComment.entity.dao.SectionContentCommentDO;
import team.seekers.hula.course.resource.sectionContentComment.entity.dto.SectionContentCommentQueryManageDTO;
import team.seekers.hula.course.resource.sectionContentComment.entity.vo.SectionContentCommentManageListVO;
import team.seekers.hula.course.resource.sectionContentComment.mapper.SectionContentCommentMapper;
import team.seekers.hula.message.message.service.MessageService;
import team.seekers.hula.user.user.entity.dao.UserDO;
import team.seekers.hula.user.user.service.UserService;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SectionContentCommentManageService extends ServiceImpl<SectionContentCommentMapper, SectionContentCommentDO> {

    @Autowired
    @Lazy
    private SectionContentService sectionContentService;

    @Autowired
    @Lazy
    private UserService userService;

    @Autowired
    @Lazy
    private SectionContentDao sectionContentDao;

    @Autowired
    @Lazy
    private MessageService messageService;

    public PageResult<SectionContentCommentManageListVO> pageOrderByGood(SectionContentCommentQueryManageDTO queryDTO) {
        // 用节名称筛选
        LambdaQueryChainWrapper<SectionContentDO> sectionContentQuery = sectionContentDao.lambdaQuery();
        sectionContentQuery.eq(SectionContentDO::getCourseId,queryDTO.getCourseId());
        if (!StringUtils.isEmpty(queryDTO.getSectionContentName())){
            sectionContentQuery.like(SectionContentDO::getName,queryDTO.getSectionContentName());
        }
        List<String> sectionContentId = sectionContentQuery
                .select(SectionContentDO::getId).list()
                .stream()
                .map(SectionContentDO::getId)
                .collect(Collectors.toList());

        LambdaQueryChainWrapper<SectionContentCommentDO> lambdaQuery = lambdaQuery();
        if(sectionContentId.size() == 0){
            return new PageResult<>();
        }
        lambdaQuery.in(SectionContentCommentDO::getSectionContentId,sectionContentId);
        // 用于审核筛选
        if(!StringUtils.isEmpty(queryDTO.getId())){
            lambdaQuery.eq(SectionContentCommentDO::getId,queryDTO.getId());
        }

        if(!StringUtils.isEmpty(queryDTO.getBegin())){
            lambdaQuery.ge(SectionContentCommentDO::getGmtCreate,queryDTO.getBegin());
        }
        if(!StringUtils.isEmpty(queryDTO.getEnd())){
            lambdaQuery.le(SectionContentCommentDO::getGmtCreate,queryDTO.getEnd());
        }

        //查询，评论内容关键词，用户id

        if (!StringUtils.isEmpty(queryDTO.getSectionContentComment())){
            lambdaQuery.like(SectionContentCommentDO::getContent,queryDTO.getSectionContentComment());
        }
        if(!StringUtils.isEmpty(queryDTO.getUserId())){
            lambdaQuery.eq(SectionContentCommentDO::getUserId,queryDTO.getUserId());
        }

        lambdaQuery.orderByDesc(SectionContentCommentDO::getWind);

        Page<SectionContentCommentDO> page = lambdaQuery.page(new Page<>(queryDTO.getPageNow(), queryDTO.getPageSize()));
        long total = page.getTotal();
        List<SectionContentCommentDO> records = page.getRecords();
        if(records == null){
            return new PageResult<>();
        }
        List<SectionContentCommentManageListVO> list = new ArrayList<>();

        for (SectionContentCommentDO record : records) {
            SectionContentCommentManageListVO sectionContentCommentManageListVO = new SectionContentCommentManageListVO();
            BeanUtils.copyProperties(record,sectionContentCommentManageListVO);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss");
            String time = simpleDateFormat.format(record.getGmtCreate());
            sectionContentCommentManageListVO.setGmtCreate(time);
            UserDO userDO = userService.getById(record.getUserId());
            if(userDO == null){
                return new PageResult<>();
            }
            sectionContentCommentManageListVO.setNickname(userDO.getNickname());
            sectionContentCommentManageListVO.setAvatar(userDO.getAvatar());
            SectionContentDTO sectionContentDTO = sectionContentService.queryById(Long.valueOf(record.getSectionContentId()));
            if(sectionContentDTO == null){
                return new PageResult<>();
            }
            sectionContentCommentManageListVO.setSectionName(sectionContentDTO.getName());
            if(record.getVisibility()==0){
                sectionContentCommentManageListVO.setVisibilityText("仅自己可见");
            }else if(record.getVisibility()==1){
                sectionContentCommentManageListVO.setVisibilityText("仅关注可见");
            }else if(record.getVisibility()==2){
                sectionContentCommentManageListVO.setVisibilityText("全部可见");
            }else if(record.getVisibility()==3){
                sectionContentCommentManageListVO.setVisibilityText("经典问题");
            }else{
                sectionContentCommentManageListVO.setVisibilityText("未知权限");
            }

            //sectionContentCommentManageListVO.setAvatar(userDO.getAvatar());
            list.add(sectionContentCommentManageListVO);
        }
        return new PageResult<>(total,list);
    }

    public void updata(String commentId,Integer type,String replay){
        SectionContentCommentDO sectionContentCommentDO = getById(commentId);
        sectionContentCommentDO.setVisibility(type);
        sectionContentCommentDO.setReply(replay);
        updateById(sectionContentCommentDO);
    }
}
